//
//  ChooseReportTypeVC.m
//  HNJYB
//
//  Created by panshen on 2017/10/23.
//  Copyright © 2017年 Mag1cPanda. All rights reserved.
//

#import "ChooseReportTypeVC.h"
#import "AccidentHeader.h"
#import "ReportTypeCell.h"
#import "BXBARightCell.h"
#import "OneCompanyVC.h"
#import "MoreCompanyVC.h"
#import "BXTypeView.h"

@interface ChooseReportTypeVC ()
<UITableViewDataSource,
UITableViewDelegate>
{
    AccidentHeader *header;
    UITableView *table;
    NSArray *iconArr;
    
    NSArray *comArr;
    NSDictionary *ownDic;
    NSDictionary *otherDic;
    
    UIView *bodyView;
    BXTypeView *typeView;
    
    NSString *cpstype;//保险类型
    
    NSString *reportway;//报案类型
}

@end

@implementation ChooseReportTypeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"请选择你的情形";
    
    iconArr = @[@"tips08", @"tips07"];
    
    header = [[AccidentHeader alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];
    header.icon.image = [UIImage imageNamed:@"IP013"];
    header.topLab.text = @"请您在保证自身安全的情况下操作";
    header.bottemLab.text = @"请根据需要选择以下方式定损、理赔";
    [self.view addSubview:header];
    
    table = [[UITableView alloc] initWithFrame:CGRectMake(10, 100, ScreenWidth - 20, ScreenHeight - 164) style:UITableViewStylePlain];
    table.tableFooterView = [UIView new];
    table.backgroundColor = HNBackColor;
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    
    [table registerNib:[UINib nibWithNibName:@"ReportTypeCell" bundle:nil] forCellReuseIdentifier:@"ReportTypeCell"];
    
    [table registerNib:[UINib nibWithNibName:@"BXBARightCell" bundle:nil] forCellReuseIdentifier:@"BXBARightCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableView Delegate & DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        BXBARightCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BXBARightCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.icon.image = [UIImage imageNamed:iconArr[indexPath.section]];
        
        return cell;
    }
    
    else {
        ReportTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReportTypeCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.icon.image = [UIImage imageNamed:iconArr[indexPath.section]];
     
        return cell;
    }
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 20, 10)];
    view.backgroundColor = HNBackColor;
    return view;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        [self chooseCompanyType];
        reportway = @"1";
    }
    
    else {
        [self getCompanyList];
        reportway = @"0";
    }
}

#pragma mark - 选择保险公司类型
-(void)chooseCompanyType
{
    bodyView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    bodyView.backgroundColor = RGBA(0, 0, 0, 0.4);
    
    __weak typeof(self) weakSelf = self;
    typeView = [[NSBundle mainBundle] loadNibNamed:@"BXTypeView" owner:nil options:nil][0];
    typeView.frame = CGRectMake(0, 0, ScreenWidth-100, 200);
    typeView.center = self.view.center;
    typeView.confirmBtnClicked = ^(NSString *selectType) {
        cpstype = selectType;
        [weakSelf saveInsuranceType];
    };
    LRViewBorderRadius(typeView, 3, 1, [UIColor clearColor]);
    
    [bodyView addSubview:typeView];
    
    [[UIApplication sharedApplication].keyWindow addSubview:bodyView];
}

#pragma mark - 获取保险公司列表
-(void)getCompanyList
{
    NSMutableDictionary *bean = [NSMutableDictionary dictionary];
    [bean setValue:GlobleInstance.userflag forKey:@"userflag"];
    [bean setValue:GlobleInstance.appcaseno forKey:@"appcaseno"];
    [bean setValue:GlobleInstance.token forKey:@"token"];
    [bean setValue:GlobleInstance.casecarno forKey:@"casecarno"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [LSHttpManager requestUrl:HNServiceURL serviceName:@"jjappkckpSearchCpList" parameters:bean complete:^(id result, ResultType resultType) {
        
        NSLog(@"jjappkckpSearchCpList -> %@", result);
        [hud hideAnimated:YES];
        
        if ([result[@"restate"] isEqualToString:@"1"])
        {
            comArr = result[@"data"][@"AllCpsList"];
            ownDic = result[@"data"][@"own"];
            otherDic = result[@"data"][@"other"];
            
            if (GlobleInstance.caseType == HNCaseTypeOneCar) {
                OneCompanyVC *vc = [OneCompanyVC new];
                vc.comArr = comArr;
                vc.ownDic = ownDic;
                vc.reportway = reportway;
                [self.navigationController pushViewController:vc animated:YES];
            }
            
            else {
                MoreCompanyVC *vc = [MoreCompanyVC new];
                vc.comArr = comArr;
                vc.ownDic = ownDic;
                vc.otherDic = otherDic;
                vc.reportway = reportway;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
        
    }];
}

#pragma mark - 上传用户选择的保险类型
-(void)saveInsuranceType
{
    NSMutableDictionary *bean = [NSMutableDictionary dictionary];
    [bean setValue:GlobleInstance.userid forKey:@"username"];
    [bean setValue:GlobleInstance.appcaseno forKey:@"appcaseno"];
    [bean setValue:GlobleInstance.token forKey:@"password"];
    [bean setValue:cpstype forKey:@"cpstype"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [LSHttpManager requestUrl:HNServiceURL serviceName:@"saveCpstype" parameters:bean complete:^(id result, ResultType resultType) {
        
        NSLog(@"saveCpstype -> %@", result);
        [hud hideAnimated:YES];
        
        if ([result[@"restate"] isEqualToString:@"1"]) {
            [self getCompanyList];
        }
        
    }];
}


@end
